package com.hs.srm.admin.modules.harmfulsubstance.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.harmfulsubstance.dao.DeclarationFormHeadDao;
import com.hs.srm.admin.modules.harmfulsubstance.entity.DeclarationFormHeadEntity;
import com.hs.srm.admin.modules.harmfulsubstance.service.DeclarationFormHeadService;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.common.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 宣告表服务实现类
 *
 * @author ych
 * @since 2020-03-24 13:29:50
 */
@Service("declarationFormService")
public class DeclarationFormHeadServiceImpl extends ServiceImpl<DeclarationFormHeadDao, DeclarationFormHeadEntity> implements DeclarationFormHeadService {

    @Autowired
    private DeclarationFormHeadDao declarationFormHeadDao;

    /**
     * 列表数据查询
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //宣告表号
        String formCode = (String) params.get("formCode");

        //模块名称（物料描述）
        String materialDesc = (String) params.get("materialDesc");

        // 生效状态:0，未生效；1，生效；2，无效
        String validStatus = (String) params.get("validStatus");

        // 预警状态：0：正常；1，预警中；2，已超期；-1，审核不通过，不预警（审批通过的才进行预警）
        String warningStatus = (String) params.get("warningStatus");

        //专用号(物料号)
        String materialCode = (String) params.get("materialCode");

        //关联专用号(相关联的物料号，多个以逗号分隔)
        String relateMaterialCode = (String) params.get("relateMaterialCode");

        //申请审核工厂
        String verifyFactory = (String) params.get("verifyFactory");

        // 节点状态:0，厚声待审核；1，厚声审核通过；2，厚声审核退回；3，厚声已淘汰
        String nodeStatus = (String) params.get("nodeStatus");

        //宣告开始日期
        String declarationStartDate = (String) params.get("declarationStartDate");

        //宣告录入结束日期
        String declarationEndDate = (String) params.get("declarationEndDate");

        //有效开始日期
        String expireStartDate = (String) params.get("expireStartDate");

        //有效录入结束日期
        String expireEndDate = (String) params.get("expireEndDate");

        //校核开始日期
        String reviewStartDate = (String) params.get("reviewStartDate");

        //宣告录入结束日期
        String reviewEndDate = (String) params.get("reviewEndDate");

        //供应商账号
        String vendorCode = (String) params.get("username");

        Page page = this.selectPage(new QueryParam<DeclarationFormHeadEntity>(params).getPage(),
                new EntityWrapper<DeclarationFormHeadEntity>()
                        .eq(StringUtils.isNotBlank(formCode), "form_code", formCode)
                        .like(StringUtils.isNotBlank(materialDesc), "material_desc", materialDesc)
                        .like(StringUtils.isNotBlank(relateMaterialCode), "relate_material_code", relateMaterialCode)
                        .eq(StringUtils.isNotBlank(materialCode), "material_code", materialCode)
                        .eq(StringUtils.isNotBlank(warningStatus), "warning_status", warningStatus)
                        .like(StringUtils.isNotBlank(verifyFactory), "verify_factory", verifyFactory)
                        .eq(StringUtils.isNotBlank(validStatus), "valid_status", validStatus)
                        .eq(StringUtils.isNotBlank(nodeStatus), "node_status", nodeStatus)
                        .ge(StringUtils.isNotBlank(declarationStartDate), "declaration_date", declarationStartDate)
                        .le(StringUtils.isNotBlank(declarationEndDate), "declaration_date", declarationEndDate)
                        .ge(StringUtils.isNotBlank(expireStartDate), "expire_date", expireStartDate)
                        .le(StringUtils.isNotBlank(expireEndDate), "expire_date", expireEndDate)
                        .ge(StringUtils.isNotBlank(reviewStartDate), "review_date", reviewStartDate)
                        .le(StringUtils.isNotBlank(reviewEndDate), "review_date", reviewEndDate)
                        .eq(StringUtils.isNotBlank(vendorCode), "vendor_code", vendorCode)
                        .orderBy("valid_status,expire_date")
        );

        return new PageUtils(page);
    }


    /**
     * 超过当前日期设置失效
     *
     * @param now 当前日期
     * @return
     */
    @Override
    public int setExpiredStatus(String now) {
        return declarationFormHeadDao.setExpiredStatus(now);
    }

    /**
     * 到期前两周预警
     *
     * @param after 当前日期加两周
     * @return
     */
    @Override
    public int setWarningStatus(String after) {
        return declarationFormHeadDao.setWarningStatus(after);
    }
}